<?php
/**
 * @param $url
 * @param $data
 * @param string $method
 * @param array $header
 * @param bool $return
 * @return mixed
 */
function  curlRequest($url,$data = '',$method = 'GET',$header = [],$return = true)
{
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);

    if($method == "GET"){
        if(!empty($data)){
              if(is_array($data))
              {
                  $queryString = http_build_query($data);
                  if(strpos($url,"?") != false)
                  {
                      $url = rtrim($url,"&"). "&".$queryString;
                  }else{
                      $url = $url."?".$queryString;
                  }
              }else{
                  if(strpos($url,"?") != false)
                  {
                      $url = rtrim($url,"&"). "&".$data;
                  }else{
                      $url = $url."?".$data;
                  }
              }
        }
    }elseif($method == "POST")
    {
        curl_setopt($ch,CURLOPT_POST,true);
    }else{
        //自定义请求方法的处理
    }
    if(!empty($data)){
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    }
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,$return);

    $urlInfo = parse_url($url);
    if($urlInfo['scheme'] == "https" )
    {
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    }

    if(is_array($header) && !empty($header)){
        curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    }
    $result = curl_exec($ch);

    curl_close($ch);
    return $result;
}

function getCityCnNameByIP($ip,$ak = "Foj6hpMYFWTXIWyA5dzXwenz",$options = ['coor'=>'bd09ll'])
{
    $queryString = http_build_query($options);
    $url = "http://api.map.baidu.com/location/ip?ak=$ak&ip=$ip&".$queryString;
    $jsonContent =  file_get_contents($url);
    $jsonArray = json_decode($jsonContent,true);
    if(isset($jsonArray['content']['address_detail']['city']))
    {
        return rtrim($jsonArray['content']['address_detail']['city'],"市");
    }else{
        return false;
    }
}